<html>
<head>
    <title>cometd-yui3 test cases - subscribe, unsubscribe etc.</title>
    <!--<script type="text/javascript" src="http://yui.yahooapis.com/3.0.0pr2/build/yui/yui-min.js"></script>-->
    <!--<script type="text/javascript" src="../cometd-yui3/cometd-yui3.js"></script>-->
    <script src="../cometd-yui3/cometd-yui3-loader.js" type="text/javascript"></script>
</head>
<body class="yui-skin-sam">
<h1 id="test">
    <script>document.write(document.title)</script>
</h1>
<script type="text/javascript">
    comet.YUI({base:'../yui3/',cometBase:'../cometd-yui3/'}).use('yuitest', 'node', 'console', 'comet', function(Y) {

        Y.namespace('comet.tests')
        Y.comet.tests.Tests = new Y.Test.Case({
            name:'Subscribe Tests',
            setUp:function() {
                this.data = {
                    key:'value'
                }
            },
            tearDown:function() {
                delete this.data;
                Y.Cookie.remove('BAYEUX_BROWSER', {path:'/'})
            },

            testSimpleSubscribeUnsubscribe:function() { 
                var comet = new Y.Comet({uri:'.cometd'});

                comet.on('subscribe', function(r) {
                    this.resume(function() {
                        //Y.Assert.areEqual('connected', comet.getClientState())
                        Y.Assert.isTrue(r.successful)
                        Y.Assert.areEqual('/testSimpleSubscribeUnsubscribe', r.subscription)
                        comet.unsubscribe({channel:'/testSimpleSubscribeUnsubscribe'})
                        this.wait(function() { Y.Assert.fail('fail to trigger the \'unsubscribe\' event with 1000ms')}, 1000);
                    })
                }, this)

                comet.on('unsubscribe', function(r) {
                    this.resume(function() {
                        Y.Assert.areEqual('connected', comet.getClientState())
                        Y.Assert.isTrue(r.successful)
                        Y.Assert.areEqual('/testSimpleSubscribeUnsubscribe', r.subscription)
                        Y.later(500, comet, comet.disconnect)
                        Y.later(1000, this, function() {
                            Y.Assert.areEqual('unconnected', comet.getClientState())
                        })
                    })
                }, this)

                comet.subscribe({channel:'/testSimpleSubscribeUnsubscribe'})
                //TODO it seems better to use time rather than event to chain the test flow. or at least to remove the
                // listener immediately after fired
                this.wait(function() { Y.Assert.fail('fail to trigger the \'subscribe\' event with 1000ms')}, 1000);
            },

            testSubscribePublishListener:function() {
                var name = '/testSubscribePublishListener'
                var comet = new Y.Comet({uri:'.cometd'});
                var listener = Y.bind(function(m) {
                    this.resume(function() {
                        Y.Assert.areEqual(name, m.data.key);
                        Y.later(500, comet, comet.disconnect)
                        Y.later(1000, this, function() {
                            Y.Assert.areEqual('unconnected', comet.getClientState())
                        })
                    })

                }, this)

                comet.subscribe({channel:name, listener:listener})

                this.wait(function() {
                    comet.batch({channel:name, data:{key:name}})
                    comet.flush();
                    this.wait(function() { Y.Assert.fail('fail to trigger the \'listener\' event with 1000ms')}, 1000);
                }, 2000)

            },

            XLastFunction:function() {
            }
        })
        //TODO add test cases about handleAdvice

        Y.comet.tests.DevTests = new Y.Test.Case({
            name: 'Development Tests',
            setUp:function() {
                this.data = {
                }
            },
            tearDown:function() {
                delete this.data;
            },

            XLastFunction:function() {
            }
        })

        //TODO: https://sourceforge.net/tracker2/?func=detail&atid=836476&aid=2431381&group_id=165715
        var console = new Y.Console()
        console.get('contentBox').setStyle('width', (Y.get('body').get('docWidth') * 3 / 4) + 'px')
        console.render()
        console.get('contentBox').query('div .yui-console-bd').setStyle('height', (Y.get('body').get('docHeight') * 4 / 5) + 'px')
        console.on('entry', function(e) {
            if (e.message.source !== 'TestRunner') {
                e.preventDefault()
            }
        })

        Y.Test.Runner.add(Y.comet.tests.DevTests);
        if (!Y.Comet.any(Y.comet.tests.DevTests, function(v, k) {
            return /^test/.test(k)
        })) {
            // if there are any development tests, normal test won't run
            Y.Test.Runner.add(Y.comet.tests.Tests);
        }
        Y.Test.Runner.run();
    })
</script>


</body>
</html>